<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="房源号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="no">
              <a-input v-model="model.no"placeholder="请输入房源号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产权证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="propertyNo">
              <a-input v-model="model.propertyNo"placeholder="请输入产权证号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="门牌号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="houseNo">
              <a-input v-model="model.houseNo"placeholder="请输入门牌号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="宿舍名称（按栋填报）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name"placeholder="请输入宿舍名称（按栋填报）" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="班组名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupName">
              <a-input v-model="model.groupName"placeholder="请输入班组名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单元" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input v-model="model.unit"placeholder="请输入单元" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="楼层" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="floor">
              <a-input-number v-model="model.floor"placeholder="请输入楼层" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="房间号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roomNo">
              <a-input v-model="model.roomNo"placeholder="请输入房间号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="宿舍类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="room_type" placeholder="请选择宿舍类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="面积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="area">
              <a-input-number v-model="model.area"placeholder="请输入面积" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="建造年月" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buildTime">
              <j-date placeholder="请选择建造年月" v-model="model.buildTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="收费标准" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fare">
              <a-input-number v-model="model.fare"placeholder="请输入收费标准" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否有人住（0无居住，1有居住）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="islive">
              <j-dict-select-tag type="list" v-model="model.islive" dictCode="yn" placeholder="请选择是否有人住（0无居住，1有居住）" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pic">
              <j-image-upload isMultiple  v-model="model.pic"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="床位数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bedNum">
              <a-input-number v-model="model.bedNum"placeholder="请输入床位数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="入住人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="livedNum">
              <a-input-number v-model="model.livedNum"placeholder="请输入入住人数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态（正常，维修中，黑名单,被框定,被锁定）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <j-dict-select-tag type="list" v-model="model.state" dictCode="room_state" placeholder="请选择状态（正常，维修中，黑名单,被框定,被锁定）" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备用字段1" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="temp1">
              <a-input v-model="model.temp1"placeholder="请输入备用字段1" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备用字段2" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="temp2">
              <a-input v-model="model.temp2"placeholder="请输入备用字段2" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "ZnzfRoomModal",
    components: {
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
            bedNum:0,
            livedNum:0,
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
           no: [
              { required: true, message: '请输入房源号!'},
           ],
           islive: [
              { required: true, message: '请输入是否有人住（0无居住，1有居住）!'},
           ],
           bedNum: [
              { required: true, message: '请输入床位数!'},
           ],
           livedNum: [
              { required: true, message: '请输入入住人数!'},
           ],
           state: [
              { required: true, message: '请输入状态（正常，维修中，黑名单,被框定,被锁定）!'},
           ],
        },
        url: {
          add: "/projectroom/znzfProject/addZnzfRoom",
          edit: "/projectroom/znzfProject/editZnzfRoom",
        }

      }
    },
    created () {
    //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model['projectId'] = this.mainId
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>
